/***********************************************************************************
Name:  BookingDetailsTrgController
Copyright © 2012  Lodha Group
===================================================================================
===================================================================================
Purpose:
-------
Controller class for Trigger developed on Booking_Details__c Object
===================================================================================
===================================================================================
History
-------
VERSION    AUTHOR                DATE             DETAIL              
1.0        CG Dev Team           18/06/2012      INITIAL DEVELOPMENT
************************************************************************************/
public class BookingDetailsTrgController{
    
    
    
    public static String NONE='NONE';
    public static String CRB_DAM='CRB + DAM';
    public static String CRB_SCUD='CRB + SCUD';
    public static String CRB_CORPORATE='CRB + Corporate';
    public static String REGISTERED='Registered Customer';
    
    public static String CREDIT_TRANSACTION='Credit';
    public static String DEBIT_TRANSACTION='Debit';
     
    public static String POINTS = 'Points';
    public static String LADDER = 'Ladder';
    
    
    private static Boolean TriggerExecute=false;
    
    public static Boolean isTriggerExecuted(){
        return TriggerExecute;
    }

    public static void TriggerExecuted(){
        TriggerExecute = true;
    }    
    /**************************************************************************************
    Description :  
    **************************************************************************************/
    public void MapBookedAccountAndOpportunity(List<Booking_Details__c> BookingDetailsList){
     try{
        system.Debug('#  -> BookingDetailsTrgController.MapBookedAccountAndOpportunity');
        Set<String> SrcRefFlatCodes = new Set<String>();
        Set<String> CRNSet = new Set<String>();
       
        
        
        //Loyalty_Detail__c ld = new Loyalty_Detail__c(Loyalty_Id__c='XYZ123',Current_casa_ladder__c='10' );
        //insert ld;
        //Projects__c prj = new Projects__c(Name = 'Test Project');
        //insert prj;
       
        for(Booking_Details__c Booking : BookingDetailsList){
                CRNSet.add(Booking.CRN__c);
                CRNSet.add(Booking.Referrer_Booking_Id__c);
        }

        Map<String,Booking_Details__c> BookingDetailsMap = getBookingDetailsMap(CRNSet);
        for(Booking_Details__c Booking : BookingDetailsList){
            Booking_Details__c bk = BookingDetailsMap.get(Booking.CRN__c);
            system.Debug('# 1'+bk.Points_Allocated__c +' 2: '+ bk.Points_Revoked__c +' 3:'+bk.Account__r.Status__c);
            if(!bk.Points_Allocated__c || (!bk.Points_Revoked__c && 'Cancelled'.equals(bk.Account__r.Status__c))){
                SrcRefFlatCodes.add(getSrcRefFlatCode(bk));
            }
        }
if(!SrcRefFlatCodes.isEmpty()){       
            
        Map<String,Double> PaymentRcvdPerCRNMap = getPaidAmountPerCRN(CRNSet);
        Map<String,Consideration_Value_Points__c>  CVPCstStng = getConsiderationValuePoints();
        Map<String,ReferralDetails__c> ReferralDetailsMap = getReferralDetails(SrcRefFlatCodes);
        Map<String,Loyalty_Points_History__c> LoyaltyPointsHistoryMap = getLoyaltyPointsHistoryMap(ReferralDetailsMap.values());
        
        List<ReferralDetails__c> ReferralDetailToUpdate = new List<ReferralDetails__c>();
        List<Loyalty_Points_History__c> LoyaltyPointsToInsert = new List<Loyalty_Points_History__c>();
        List<Booking_Details__c> BookingDetailsToUpdate = new List<Booking_Details__c>();
         
         
        for(Booking_Details__c Booking : BookingDetailsList){
            Booking = BookingDetailsMap.get(Booking.CRN__c);              
            String SrcRefFlatCode =  getSrcRefFlatCode(Booking);         
            ReferralDetails__c RefDet =  ReferralDetailsMap.get(SrcRefFlatCode);
            if(null != RefDet){
                RefDet.Referral_Booked_Account__c = Booking.Account__c;
                if('Cancelled'.equals(Booking.Account__r.Status__c)){
                    RefDet.Status__c = 'Booked Account Cancelled';
                }else{
                    RefDet.Status__c ='Booked Account Created';
                }
                            
                ReferralDetailToUpdate.add(RefDet);
                
            }
            

         Boolean BookingModified = false;   
         system.Debug('#Point Allocation Starts On : '+Booking.Id);
         if(!'Cancelled'.equals(Booking.Account__r.Status__c)){
            if(!Booking.Points_Allocated__c){
            //Points are not allocated as of now
            system.Debug('#Points are not allocated as of now : '+Booking.Id);
                if(Booking.Account__r.Loyalty_Details__r.Loyalty_Id__c !=  Booking.Referrer_Loyalty_Id__c){
                //Not an Self refferal
                system.Debug('#Not an Self refferal : '+Booking.Id);
                    Booking_Details__c SourceBooking = BookingDetailsMap.get(Booking.Referrer_Booking_Id__c);
                    if(SourceBooking != null){
                        system.Debug('# SourceBooking '+SourceBooking);
                        
                        String SourceProject = SourceBooking.Project__c;
                        Date SourceAppDate = SourceBooking.Application_Date__c;
                        String RefProject = Booking.Project__c;
                        Date RefAppDate = Booking.Application_Date__c;
                        
                        system.Debug('# Source Project :'+SourceProject +' ->  DATE : '+SourceAppDate);
                        system.Debug('# Referrence Project :'+RefProject +' ->  DATE : '+RefAppDate);
                        
                        if(SourceProject != RefProject || SourceAppDate!= RefAppDate){
                        //Project Code and Application Date of Source and Referred are Different
                        system.Debug('#Project Code and Application Date of Source and Referred are Different : '+Booking.Id);
                            
                            Double SourceCV = SourceBooking.Consideration_Value__c;
                            Double RefferedCV = Booking.Consideration_Value__c;
                            
                            Double PaidSourceCV = PaymentRcvdPerCRNMap.get(SourceBooking.CRN__c);
                            Double PaidRefferedCV = PaymentRcvdPerCRNMap.get(Booking.CRN__c);
                            
                            if(SourceCV != null && RefferedCV !=null && PaidSourceCV !=null && PaidRefferedCV != null){
                                if(PaidSourceCV >= (SourceCV*0.099) && PaidRefferedCV >= (RefferedCV*0.099)){
                                //Payemnt made more than 9.9%
                                system.Debug('#Payemnt made more than 9.9% : '+Booking.Id);
                                    Double point = 0;
                                    system.Debug('#RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c : '+RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c);
                                    if(POINTS.equals(RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c)){
                                        point = getLoyaltyPoint(Booking.Consideration_Value__c, CVPCstStng);
                                    }else if(LADDER.equals(RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c)){
                                        point = 1;
                                    }
                                    
                                    if(NONE.equals(Booking.Customer_Group__c) || CRB_DAM.equals(Booking.Customer_Group__c) || CRB_SCUD.equals(Booking.Customer_Group__c) ){
                                    //Both Source and Referred gets the Loyalty Points
                                    system.Debug('# Both Source and Referred gets the Loyalty Points'+Booking.Id);
                                        Loyalty_Points_History__c RefPoint = AllocatePoints(Booking.Account__r.Id,
                                                                                            Booking.Account__r.Loyalty_Details__r.Id,
                                                                                            RefDet.Id,
                                                                                            RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c,
                                                                                            point);
                                        if(RefPoint != null){
                                            LoyaltyPointsToInsert.add(RefPoint);
                                            Booking.Trigger_Date__c=system.now();
                                            Booking.Points_Allocated__c=true;
                                            Booking.Trigger_Msg__c = RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c +' Allocated';
                                            BookingModified = true;                                         
                                        }                                                                                               
                                        Loyalty_Points_History__c SrcPoint = AllocatePoints(SourceBooking.Account__r.Id,
                                                                                            SourceBooking.Account__r.Loyalty_Details__r.Id,
                                                                                            RefDet.Id,
                                                                                            RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c,
                                                                                            point);
                                        if(SrcPoint != null){
                                            LoyaltyPointsToInsert.add(SrcPoint);
                                        }                                                                                                                                                                                       
                                    }else if(CRB_CORPORATE.equals(Booking.Customer_Group__c)){
                                    //Only Source gets the Loyalty Points
                                    system.Debug('# Only Source gets the Loyalty Points'+Booking.Id);
                                        Loyalty_Points_History__c SrcPoint = AllocatePoints(SourceBooking.Account__r.Id,
                                                                                            SourceBooking.Account__r.Loyalty_Details__r.Id,
                                                                                            RefDet.Id,
                                                                                            RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c,
                                                                                            point);
                                        if(SrcPoint != null){
                                            LoyaltyPointsToInsert.add(SrcPoint);
                                        }                                                                                                                                   
                                    }else if(REGISTERED.equals(Booking.Customer_Group__c)){
                                    //No One get the Loyslty Points
                                    system.Debug('# No One get the Loyslty Points : REGISTERED :'+Booking.Id);
                                        Booking.Trigger_Date__c=system.now();
                                        Booking.Trigger_Msg__c=REGISTERED+ ' : No One get the Loyslty Points';
                                        BookingModified = true;                                         
                                    }
                                }else{
                                //Enough Payment is not made 9.9%
                                system.Debug('# Enough Payment is not made 9.9%'+Booking.Id);
                                    Booking.Trigger_Date__c=system.now();
                                    Booking.Trigger_Msg__c='Enough Payment is not made 9.9%';
                                    BookingModified = true;                                                                                         
                                }
                            }else{
                            //Enough Payment is not made 9.9%
                            system.Debug('# Enough Payment is not made 9.9%'+Booking.Id);
                                Booking.Trigger_Date__c=system.now();
                                Booking.Trigger_Msg__c='Enough Payment is not made 9.9%';
                                BookingModified = true;                         
                            }
                        }else{
                        //Project Code and Application Date of Source and Referred are Same
                        system.Debug('# Project Code and Application Date of Source and Referred are Same : '+Booking.Id);
                            Booking.Trigger_Date__c=system.now();
                            Booking.Trigger_Msg__c='Project Code and Application Date of Source and Referred are Same';
                            BookingModified = true;
                        }
                    }else{
                    //Invalid Source Referral Booking Id
                    system.Debug('# Invalid Source Referral Booking Id'+Booking.Id);
                        Booking.Trigger_Date__c=system.now();
                        Booking.Trigger_Msg__c='Invalid Source Referral Booking Id';
                        BookingModified = true; 
                    }
                }else{
                //Self refferel
                system.Debug('# Self refferel'+Booking.Id);
                    Booking.Trigger_Date__c=system.now();
                    Booking.Trigger_Msg__c='Self Sefferel';
                    BookingModified = true;
                }
            }
         }else{
         //Cancelling Points
         system.Debug('# Cancelling Points : '+Booking.Id);
                Booking_Details__c SourceBooking = BookingDetailsMap.get(Booking.Referrer_Booking_Id__c);
                system.Debug('# SourceBooking '+SourceBooking);
                if(SourceBooking != null){
                    String SourceKey = String.valueOf(RefDet.Id) + String.valueOf(SourceBooking.Account__r.Id);
                    system.Debug('# SourceKey '+SourceKey);
                    Loyalty_Points_History__c SourceHistory = LoyaltyPointsHistoryMap.get(SourceKey);
                    system.Debug('# SourceHistory '+SourceHistory);
                    if(SourceHistory!=null){
                        Loyalty_Points_History__c SrcPoint = RevokePoints(SourceBooking.Account__r.Id,
                                                                    SourceBooking.Account__r.Loyalty_Details__r.Id,
                                                                    RefDet.Id,
                                                                    RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c,
                                                                    SourceHistory.Points_Added__c);
                        if(SrcPoint != null){
                            LoyaltyPointsToInsert.add(SrcPoint);                                
                        }               
                    }
                if(!Booking.Points_Revoked__c){                     
                    String RefKey = String.valueOf(RefDet.Id) + String.valueOf(Booking.Account__r.Id);
                    system.Debug('# RefKey '+RefKey);
                    Loyalty_Points_History__c RefHistory = LoyaltyPointsHistoryMap.get(RefKey);
                    system.Debug('# RefKey '+RefHistory);
                    if(RefHistory != null){
                        Loyalty_Points_History__c RefPoint = RevokePoints(Booking.Account__r.Id,
                                                                    Booking.Account__r.Loyalty_Details__r.Id,
                                                                    RefDet.Id,
                                                                    RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c,
                                                                    RefHistory.Points_Added__c);
                        if(RefPoint != null){
                            LoyaltyPointsToInsert.add(RefPoint);
                            Booking.Trigger_Date__c=system.now();
                            Booking.Points_Revoked__c=true;
                            Booking.Trigger_Msg__c = RefDet.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c +' Revoked';
                            BookingModified = true;                                     
                        }           
                    }
                }   
            }               
         }   
         if(BookingModified){
            BookingDetailsToUpdate.add(Booking);
         }
            //system.Debug('#Points Allocated : '+LytPoint);
        }
        
        if(!ReferralDetailToUpdate.isEmpty()){
            Update ReferralDetailToUpdate;
        }
        if(!LoyaltyPointsToInsert.isEmpty()){
            Insert LoyaltyPointsToInsert;
        }
        if(!BookingDetailsToUpdate.isEmpty()){
            Update  BookingDetailsToUpdate;
        }
     }//-----------
              
      }catch(Exception e){
        system.Debug('#Exception In MapBookedAccountAndOpportunity : '+e);
      }
        system.Debug('#  <- BookingDetailsTrgController.MapBookedAccountAndOpportunity');   
    }
    
    
    /*****************************************************************************
    Description :
    /****************************************************************************/
    private String getSrcRefFlatCode(Booking_Details__c Booking){
        system.Debug('#  -) BookingDetailsTrgController.getSrcRefFlatCode');
        system.Debug('# Booking.Account__c : '+Booking.Account__c);
        system.Debug('# Booking.Account__r.Loyalty_Details__r : '+Booking.Account__r.Loyalty_Details__c);
        return Booking.Account__r.Loyalty_Details__r.Loyalty_Id__c+
               Booking.Referrer_Loyalty_Id__c+
               Booking.Project__c+ Booking.Wing__c+Booking.Flat_No__c;    
    } 
    
          /*****************************************************************************
    Description : This class to used for updating Source Loyalty Id and unique values.. 
    /****************************************************************************/
    public  list<Opportunity> createSrcRefFlatCode(list<Opportunity>  opps){
    	
    	list<Opportunity> updatedOpps  = new list<Opportunity>();
    	Map<Id, string> ProjectIdMaps= new Map<Id, string>();
    	Set<Id> setProjcId= new Set<Id>();
    	set<Id> leadIds = new set<Id>();
    	Map<Id, string> loyaltyIdMaps= new Map<Id, string>();
    	
    	for(Opportunity aOpp: opps){
    		setProjcId.add(aOpp.Project_Name__c);
    		leadIds.add(aOpp.Lead_ID__c);
    	}
    	
		for(ReferralDetails__c aReferral:[Select id,Lead__c , Loyalty_Detail__r.Loyalty_Id__c from ReferralDetails__c where Lead__c=:leadIds]) {
				
				loyaltyIdMaps.put(aReferral.Lead__c,aReferral.Loyalty_Detail__r.Loyalty_Id__c);
		}
		
    	
   
    	
    	if(setProjcId!=null){    		
    		for(Project_Name__c aProject:  [Select Id, Name From Project_Name__c where Id in:setProjcId]){
    			ProjectIdMaps.put(aProject.Id,aProject.Name);
    		}
    	}
    	
    	
    	for(Opportunity aOpp: opps){
    		aOpp.LoyaltySourceId__c=(loyaltyIdMaps.get(aOpp.Lead_ID__c)==null?'':loyaltyIdMaps.get(aOpp.Lead_ID__c).trim());
	    	 aOpp.Source_referral_flat_code__c=  aOpp.LoyaltyID__c+
	               aOpp.LoyaltySourceId__c+
	               ProjectIdMaps.get(aOpp.Project_Name__c).trim()+ aOpp.Wing__c+aOpp.Flat_No__c;    
	               updatedOpps.add(aOpp);
    	}
    	
    	
    	
    	 return updatedOpps;
               
    } 
    
           
    
    
    /*****************************************************************************
    Description :
    /****************************************************************************/
    private Loyalty_Points_History__c AllocatePoints(Id BookedAccountId,
                                                       Id LoyaltyDetailsId,
                                                       Id ReferenceDetailId,
                                                       String ProgramType,
                                                       Double Point){
                                                            
          return  CreateLoyaltyPoints(BookedAccountId,
                                      LoyaltyDetailsId,
                                      ReferenceDetailId,
                                      ProgramType,
                                      Point,
                                      'Credit');                                                
    }

    /*****************************************************************************
    Description :
    /****************************************************************************/
    private Loyalty_Points_History__c RevokePoints(Id BookedAccountId,
                                                       Id LoyaltyDetailsId,
                                                       Id ReferenceDetailId,
                                                       String ProgramType,
                                                       Decimal Point){
                                                            
          return  CreateLoyaltyPoints(BookedAccountId,
                                      LoyaltyDetailsId,
                                      ReferenceDetailId,
                                      ProgramType,
                                      Double.valueOf(Point),
                                      'Debit');                                             
    }
    /*****************************************************************************
    Description :
    /****************************************************************************/
    private Loyalty_Points_History__c CreateLoyaltyPoints(
                                                        Id BookedAccountId,
                                                        Id LoyaltyDetailsId,
                                                        Id ReferenceDetailId,                                                        
                                                        String ProgramType,
                                                        Double Point,
                                                        String TransactionType ){
        Loyalty_Points_History__c LoyaltyPoint = new Loyalty_Points_History__c();
        LoyaltyPoint.Account__c = BookedAccountId;
        LoyaltyPoint.Referral_Details__c = ReferenceDetailId;
        LoyaltyPoint.Loyalty_Detail__c = LoyaltyDetailsId;
        LoyaltyPoint.Loyalty_Program_Type__c =ProgramType;
        if('Credit'.equals(TransactionType)){
            LoyaltyPoint.Points_Added__c=Point;
        }else{
            LoyaltyPoint.Points_Deducted__c=Point;
        }
        return LoyaltyPoint;
    } 

    /*****************************************************************************
    Description : 
    /****************************************************************************/    
    private Map<String,ReferralDetails__c> getReferralDetails(Set<String> SrcRefFlatCodes){
        system.Debug('#  -> BookingDetailsTrgController.getReferralDetails');
        system.Debug('# SrcRefFlatCodes : '+SrcRefFlatCodes);
        Map<String,ReferralDetails__c> ReferralDetailsMap = new Map<String,ReferralDetails__c>();
        try{
            List<ReferralDetails__c> ReferralDetailsList = [Select r.Status__c, r.Referral_Booked_Account__c,
                                                            r.Opportunity__r.Source_referral_flat_code__c, 
                                                            r.Opportunity__r.Project_Name__r.Loyalty_Program_Type__c,  
                                                            r.Opportunity__c, r.Id, r.Booked_Account__c From ReferralDetails__c r 
                                                            Where r.Opportunity__r.Source_referral_flat_code__c IN:SrcRefFlatCodes];
                                                    
            for(ReferralDetails__c RefDt : ReferralDetailsList){
                ReferralDetailsMap.put(RefDt.Opportunity__r.Source_referral_flat_code__c, RefDt);
            }   
        }catch(Exception e){
            system.Debug('#Exception '+e);  
        }                                           
        system.Debug('#  <- BookingDetailsTrgController.getReferralDetails');
        return ReferralDetailsMap;  
    
    }
    
   /****************************************************************************************************************
   Description : 
   /***************************************************************************************************************/
    private Map<String,Double> getPaidAmountPerCRN(Set<String> CRNset){
        system.Debug('#  -> BookingDetailsTrgController.getPaidAmountPerCRN');
        Map<String,Double> DemadDetailsMap = new Map<String,Double>();
        try{
          if(!CRNset.isEmpty()){
            List<SObject> DemandPerAcc = [Select d.Flat_ID__r.CRN__c CRN, 
                                       Sum(d.Amount_received__c) PaidAmount 
                                       From Demands__c d 
                                       Where d.Flat_ID__r.CRN__c IN:CRNset 
                                       Group By  d.Flat_ID__r.CRN__c]; 
            for(sObject Demand : DemandPerAcc){
                String CRN = string.valueOf(Demand.get('CRN'));
                Double PaidAmount = Double.valueOf(Demand.get('PaidAmount'));
                DemadDetailsMap.put(CRN,PaidAmount);
            }                   
          }                
        }catch(Exception e){
            system.Debug('#Exception : '+e);
        }
        system.Debug('#  <- BookingDetailsTrgController.getPaidAmountPerCRN');
        return DemadDetailsMap;
    }

   /****************************************************************************************************************
   Description : 
   /***************************************************************************************************************/            
   private Map<String,Consideration_Value_Points__c> getConsiderationValuePoints(){
    system.Debug('#  -> BookingDetailsTrgController.getConsiderationValuePoints');
    Map<String,Consideration_Value_Points__c> CVPmap = new Map<String,Consideration_Value_Points__c>(); 
    try{
        for(Consideration_Value_Points__c CVP:Consideration_Value_Points__c.getAll().values()){
            if(CVP.Active__c){
                CVPmap.put(CVP.Name,CVP);
            }
        }
    }catch(Exception e){
        system.Debug('#Exception '+e);  
    }
    system.Debug('#  <- BookingDetailsTrgController.getConsiderationValuePoints');
    return CVPmap;
   }   
   
   /****************************************************************************************************************
   Description : 
   /***************************************************************************************************************/       
   private Double  getLoyaltyPoint(Decimal ConsiderationValue,Map<String,Consideration_Value_Points__c>  CVPCstStng){
    system.Debug('#  -> BookingDetailsTrgController.getCVPoint');
        Double Point = 0;
        try{
            if(ConsiderationValue != null){
                Double OneCrore = 10000000;
                ConsiderationValue = ConsiderationValue / OneCrore;
                for(Consideration_Value_Points__c CVP : CVPCstStng.values()){
                	system.Debug('# ConsiderationValue : '+ConsiderationValue +' : CVP.CV_From__c = '+CVP.CV_From__c +' CVP.CV_To__c = '+CVP.CV_To__c);
                    if(CVP.CV_From__c <= ConsiderationValue && CVP.CV_To__c > ConsiderationValue){
                        Point=CVP.Credits__c;
                        system.Debug('# ---> ConsiderationValue : '+ConsiderationValue +' : CVP.CV_From__c = '+CVP.CV_From__c +' CVP.CV_To__c = '+CVP.CV_To__c);
                        break;
                    }
                }   
            }   
        }catch(Exception e){
            system.Debug('#Exception '+e);      
        }
        system.Debug('#  <- BookingDetailsTrgController.getCVPoint');               
        return Point;
   }
   
   /****************************************************************************************************************
   Description : 
   /***************************************************************************************************************/
   private Map<String,Booking_Details__c> getBookingDetailsMap(Set<String> CRNset){
        system.Debug('#  -> BookingDetailsTrgController.getBookingDetailsMap');
        Map<String,Booking_Details__c> BookingDetailsMap = new Map<String,Booking_Details__c>();
        try{
            if(!CRNset.isEmpty()){
                List<Booking_Details__c> BookingDetailsList = [Select b.CRN__c, b.Account__r.Loyalty_Details__c, b.Account__r.Status__c, 
                                                             b.Account__r.Loyalty_Details__r.Loyalty_Id__c, b.Points_Allocated__c,
                                                             b.Points_Revoked__c, b.Customer_Group__c, b.Referrer_Booking_Id__c,
                                                             b.Referrer_Loyalty_Id__c,b.Wing__c, b.Flat_No__c, b.Application_Date__c,
                                                             b.Consideration_Value__c, b.Account__r.Id, b.Account__c, b.Project__c, 
                                                             b.Account__r.Loyalty_Details__r.Id 
                                                             From Booking_Details__c b Where b.CRN__c IN:CRNset];
                for(Booking_Details__c BkD : BookingDetailsList){
                    BookingDetailsMap.put(BkD.CRN__c, BkD);
                }                                                              
            }
        }catch(Exception e){
            system.Debug('#Exception '+e);
        }
        system.Debug('#  <- BookingDetailsTrgController.getBookingDetailsMap');
        return BookingDetailsMap;
   } 
   
   /****************************************************************************************************************
   Description : 
   /***************************************************************************************************************/   
   private Map<String,Loyalty_Points_History__c> getLoyaltyPointsHistoryMap(List<ReferralDetails__c> ReferralDetailsIds){
    system.Debug('#  -> BookingDetailsTrgController.getLoyaltyPointsHistoryMap');
    Map<String,Loyalty_Points_History__c> LoyaltyPointsHistoryMap = new Map<String,Loyalty_Points_History__c>();
    try{
        List<Loyalty_Points_History__c> LoyaltyPointsHistoryList = [Select l.Referral_Details__c, l.Referral_Details__r.Id, l.Account__r.Id, l.Points_Deducted__c, l.Points_Added__c, 
                                                                    l.Loyalty_Program_Type__c, l.Account__c 
                                                                    From Loyalty_Points_History__c l Where l.Referral_Details__c IN:ReferralDetailsIds];
        for(Loyalty_Points_History__c lph : LoyaltyPointsHistoryList){
            String Key = String.valueOf(lph.Referral_Details__r.Id) + String.valueOf(lph.Account__r.Id);
            LoyaltyPointsHistoryMap.put(Key,lph);
        }                                                                   
    }catch(Exception e){
        system.Debug('#Exception '+e);
    }
    system.Debug('#  <- BookingDetailsTrgController.getLoyaltyPointsHistoryMap');
    return LoyaltyPointsHistoryMap;
   }
     
}